/**********************************************
 * 
 * File Name				:	MenuServiceImpl.java
 * 
 * Last Changed Date		: 	$Date: 2009-08-06 14:51:00 +0800 (星期四, 06 八月 2009) $
 * 
 * Last Changed Revision	:	$Revision: 92 $
 * 
 * Last Changed Author		:	$Author: cfanboy $
 * 
 * File Header URL			:	$HeadURL: svn://www.svnhost.cn/Busi-Building/code/branch/src/com/busibuilding/administrate/menu/services/MenuServiceImpl.java $
 * 
 * ********************************************
 */

package com.busibuilding.administrate.menu.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.busibuilding.administrate.menu.dao.MenuDAO;
import com.busibuilding.administrate.menu.vo.MenuCVO;
import com.busibuilding.common.exceptions.DAOException;
import com.busibuilding.common.exceptions.ServiceException;
import com.busibuilding.common.services.CommonServicesImpl;


import com.busibuilding.common.vo.ICommonVO;
import com.busibuilding.utils.StringUtil;

/**
 * Class Name: MenuServiceImpl.java
 * Description: 
 * @author YU Zhipeng
 * @version 1.0
 * @since 1.0
 * Date: 2009-7-26
 */
@Transactional(rollbackForClassName={"Exception"})
public class MenuServiceImpl extends CommonServicesImpl implements MenuService {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -468572759431012683L;
	private static Logger logger = LogManager.getLogger(MenuServiceImpl.class);
	
	private MenuDAO dao;
	
	
	public void setDao(MenuDAO dao) {
		this.dao = dao;
	}

	@Override
	public int createMenu(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.insert(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_INSERT_MENU, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_INSERT_MENU, e);
		}
		return id;
	}

	@Override
	public int deleteMenu(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.delete(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_DELETE_MENU, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_DELETE_MENU, e);
		}
		return id;
	}

	@Override
	public List<MenuCVO> searchMenu(ICommonVO vo) throws ServiceException {
		List<MenuCVO> resultList = new ArrayList<MenuCVO>();
		try {
			resultList = dao.searchMenuByCondition(vo);			
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_MENU, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_MENU, e);
		}
		return resultList;
	}

	@Override
	public List<MenuCVO> searchMenuByPagination(ICommonVO vo) throws ServiceException {
		List<MenuCVO> resultList = new ArrayList<MenuCVO>();
		try {
			resultList = dao.searchMenuByCondition(vo, vo.getMinIndex(), vo.getMaxIndex());			
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_MENU, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SEARCH_MENU, e);
		}
		return resultList;
	}

	@Override
	public int updateMenu(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.update(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_MENU, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_MENU, e);
		}
		return id;
	}

	@Override
	public int getRecordsCount(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.getRecordsCount(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_MENU, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_UPDATE_MENU, e);
		}
		return id;
	}
	
	public boolean isParentMenu(String id) throws Exception{
		if (StringUtil.isNotEmpty(id)){
			MenuCVO param = new MenuCVO();
			param.setMenuId(Integer.parseInt(id));
			List<MenuCVO> resultList = dao.searchMenuByCondition(param);
			if (resultList.size() == 1){
				MenuCVO cvo = resultList.get(0);
				if (cvo.getParentMenuId() == null){
					return true;
				}
			}
		}
		return false;
	}

}
